﻿var UniverseEntityDrawBase = function () {

    var isDebug = false;
    this.AnimationUtility = new AnimationBase();

    this.Draw = function (universeEntity) {
    	
    	if(universeEntity.UniverseEntityType == UniverseEntityTypes.StarField) {
    		return this.DrawStarField(universeEntity);
    	}
    	else if(universeEntity.UniverseEntityType == UniverseEntityTypes.ShootingStar){
    		return this.DrawShootingStar(universeEntity);
    	}
    	
        var jqObject = $('<div class="hDUnivSim_UniverseEntity ' + universeEntity.EntityTypeName() + '">' + (isDebug ? universeEntity.Name : '') + '</div>');
        jqObject.css(GenerateCssStyle(universeEntity));

        return jqObject;
    };

    this.Refresh = function (universeEntity) {
    	if(universeEntity.UniverseEntityType == UniverseEntityTypes.Universe)
    		return;
        universeEntity.AssociatedJqObject.css(GenerateCssStyle(universeEntity));
    };

    function GenerateCssStyle(universeEntity) {

        var sizeAndPosition = universeEntity.SizeAndPosition;
        var centerPoint = universeEntity.RelativeCenter();

        var cssMapObject = {
            'width': sizeAndPosition.Size.Width + 'px'
            , 'height': sizeAndPosition.Size.Height + 'px'
            , 'left': (sizeAndPosition.Position.X - centerPoint.X) + 'px'
            , 'top': (sizeAndPosition.Position.Y - centerPoint.Y) + 'px'
            , 'position': 'absolute'
        }
        if (isDebug) {
            if (universeEntity.UniverseEntityType <= UniverseEntityTypes.Star)
                $.extend(cssMapObject, {
                    "background": "#000000"
                });
            else
                $.extend(cssMapObject, {
                    "border": "solid 1px #000000"
                });

        }
            if (universeEntity.UniverseEntityType == UniverseEntityTypes.Galaxy && universeEntity.DepthPercent() != 1) {
            $.extend(cssMapObject, {
                "-webkit-transform": 'scale(' + universeEntity.DepthPercent() + ')', '-webkit-transform-origin': 'center center'
                    , "-moz-transform": 'scale(' + universeEntity.DepthPercent() + ')', '-moz-transform-origin': 'center center'
                    , "-ms-transform": 'scale(' + universeEntity.DepthPercent() + ')', '-ms-transform-origin': 'center center'
                    , "transform": 'scale(' + universeEntity.DepthPercent() + ')', 'transform-origin': 'center center'
            });
        }

        return cssMapObject;
    };

    this.DrawStarField = function(starField) {

        if (starField == null)
            return null;

        var jqObject = $('<div class="hDUnivSim_UniverseEntity ' + starField.EntityTypeName() + '"></div>');
        jqObject.css({
            'opacity': 0.5
        });

        var widthRange = new Range(0, starField.SizeAndPosition.Size.Width);
        var heightRange = new Range(0, starField.SizeAndPosition.Size.Height);

        for (var index = 0; index < starField.StarCountRange.Random(); index++) {
            var starJq = $('<div class="hDUnivSim_UniverseEntity ' + starField.EntityTypeName() + ' Star"></div>');
            var starSize = starField.StarSizeRange.Random();
            starJq.css({
                'left': widthRange.Random() + 'px'
                , 'top': heightRange.Random() + 'px'
                , 'width': starSize + 'px'
                , 'height': starSize + 'px'
                , 'opacity': starField.StarSizeRange.Percent(starSize)
            });
            jqObject.append(starJq);
        };

        return jqObject;
    };
    
    this.DrawShootingStar = function(shootingStar){
    	if(shootingStar == null)
    		return null;
    	
    	var jqObject = $('<div class="hDUnivSim_UniverseEntity ' + shootingStar.EntityTypeName() + '"></div>');
    	jqObject.css({
            'opacity': 0.1
            , 'left': shootingStar.SizeAndPosition.Position.X + 'px'
            , 'top': shootingStar.SizeAndPosition.Position.Y + 'px'
        });
    	
    	var jqStarTail = $('<div class="hDUnivSim_UniverseEntity ' + shootingStar.EntityTypeName() + ' Tail"></div>');
    	jqStarTail.css({
    		'width': '1px'
        	, 'height': '1px'
            , 'left': shootingStar.RelativeCenter().X
            , 'top': shootingStar.RelativeCenter().Y
            , '-webkit-transform-origin': 'left center', '-webkit-transform': 'rotate(' + (shootingStar.MovementVector.AngleDeg() - 180) + 'deg)'
            , '-moz-transform-origin': 'left center', '-moz-transform': 'rotate('+(shootingStar.MovementVector.AngleDeg() - 180)+'deg)'
            , '-ms-transform-origin': 'left center', '-ms-transform': 'rotate('+(shootingStar.MovementVector.AngleDeg() - 180)+'deg)'
            , 'transform-origin': 'left center', 'transform': 'rotate('+(shootingStar.MovementVector.AngleDeg() - 180)+'deg)'
        });
    	jqObject.append(jqStarTail);
    	
    	return jqObject;
    };

};